//
//  TLNavgationController.m
//  Papa
//
//  Created by wf on 16/7/16.
//  Copyright © 2016年 tongli. All rights reserved.
//

#import "TLNavgationController.h"

@interface TLNavgationController ()

@end

@implementation TLNavgationController

//设置电池栏高亮显示
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

//执行该类第一个方法前执行，且只执行一次
+ (void)initialize {
    //获取当前的导航条
    UINavigationBar *navBar = [UINavigationBar appearance];
    [navBar setBackgroundImage:[UIImage imageNamed:@"背景_02"] forBarMetrics:UIBarMetricsDefault];
    
    //设置文字颜色
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //获取当前的导航按钮
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    item.tintColor = [UIColor whiteColor];
    //文字大小
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

//重写导航控制器的push，设置子控制器隐藏tabBar，自定义后退按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSInteger count = self.childViewControllers.count;
    //如果执行到这个地方，判断里面有一个控制器的话，代表当前要往导航控制器里push的是第二个控制器
    if (count >= 1) {
        //1.设置子控制器隐藏tabBar
        viewController.hidesBottomBarWhenPushed = YES;
        //2.设置子控制器的自定义后退按钮
        [self addBackBtn:viewController];
    }
    //真正实现跳转
    [super pushViewController:viewController animated:YES];
}

#pragma mark 自定义后退按钮
- (void)addBackBtn:(UIViewController *)viewController {
    //开启手势后退
    self.interactivePopGestureRecognizer.delegate = (id)self;
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navigationbar_back_withtext"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    //间距
    UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixed.width = -10;
    
    viewController.navigationItem.leftBarButtonItems = @[fixed,back];
}

//点击后退时执行的方法
- (void)backClick {
    [self popViewControllerAnimated:YES];
}

@end
